<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/5/8
 * Time: 14:07
 */
namespace app\orders\controller;

use app\common\ErrorCodes;
use think\Lang;

class Client extends Base{

    /*
     * 客户列表
     * */
    public function index(){
//        $this->authVerify();
        $where = '';
        $field = 'cid,sid,client';
        $order = 'cid asc';
        $data = $this->clients()->page('*', $where, $order);
        return $this->show($data);
        //分页
        /*$this->page($data);
        $this->asgStates();
        $this->assign('clients', $data);
        return $this->fetch('cli/index');*/
    }
    /*
     * json数据
     * */
    public function json(){
        $field = 'cid,sid,client';
        $data = $this->clients()->select($field);
        $this->assign('clients', json_encode($data));
        return $this->show($data);
    }

    /*
     * 增加客户action
     * */
    public function save(){
        $this->authVerify();
        $sid = trim(input('param.sid'));
        $client = trim(input('param.client'));
        $clients['sid'] = $sid;
        $clients['client'] = $client;
        //var_dump($clients);exit();
        //判断是否已存在
        $isExist = $this->clients()->findById(array('client'=>$client));
        if($isExist){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('existed client'));
//            return $this->error(Lang::get('existed client'));
        }
        //var_dump($clients);exit();
        $result = $this->clients()->add($clients, '');
        if (!$result){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('add fail'));
//            return $this->error(Lang::get('add fail'));
        }
        return $this->show($clients, ErrorCodes::$SUCCESS, Lang::get('add success'));
//        return $this->success(Lang::get('add success'), 'Client/index');
    }
    /*
     * 删除客户
     * */
    public function del(){
        $this->authVerify();
        $cliId = input('param.cid');
        //var_dump($cliId);exit();
        $where = array('cid'=>$cliId);
        $findCliId = $this->clients()->findById($where);
        if (!$findCliId){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind client'));
//            return $this->error(Lang::get('unfind client'), 'Client/index');
        }
        $delete = $this->clients()->del($where);
        if (!$delete){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del fail'));
//            return $this->error(Lang::get('del fail'), 'Client/index');
        }
        return $this->show($findCliId, ErrorCodes::$SUCCESS, Lang::get('del success'));
//        return $this->success(Lang::get('del success'), 'Client/index');

    }

    /*
     *查询修改客户的信息，json数据
     */
    public function one(){
        $cliId = input('param.cid');
        $field = 'cid,sid,client';
        $where = array('cid'=>$cliId);
        $data = $this->clients()->select($field, $where);
        $data = $this->resetClients($data);
        return $this->show($data);
    }
    /*
     * 编辑客户信息action
     * */
    public function esave(){
        $this->authVerify();
        $cliId = input('param.cid');
        //var_dump($cliId);exit();
        $where = array('cid'=>$cliId);
        $findCliId = $this->clients()->findById($where);
        if (!$findCliId){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind client'));
//            return $this->error(Lang::get('unfind client'));
        }
        $sid = trim(input('param.sid'));
        $client = trim(input('param.client'));
        $clients['sid'] = $sid;
        $clients['client'] = $client;
        //判断是否已存在
        $isExist = $this->clients()->findById(array('cid'=>['<>', $cliId],'client'=>$client));
        if($isExist){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('existed client'));
//            return $this->error(Lang::get('existed client'));
        }
        $result = $this->clients()->update($clients, $where);
        if (!$result){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('edit fail'));
//            return $this->error(Lang::get('edit fail'));
        }
        return $this->show($clients, ErrorCodes::$SUCCESS, Lang::get('edit success'));
//        return $this->success(Lang::get('edit success'), 'Client/index');
    }


    /**
     * @return mixed
     * 模糊查询
     */
    public function search(){
        $search = input('param.search');
        //var_dump($search);exit();
        $joinTable = 'states';
        $param = 'sid';
        $data = $this->clients()->searchLike($search, $joinTable, $param);
        return $this->show($data);
        /*$this->page($data);
        $this->asgStates();
        $this->assign('clients', $data);
        return $this->fetch('cli/index');*/
    }

    /**
     * 重组-二维数组
     * @param $clients
     */
    private function resetClients($clients){
        $len = count($clients);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $clients[$i] = $this->resetClient($clients[$i]);
            }
        }
        return $clients;
    }

    /**
     * 重组-一维数组
     * @param $client
     * @return mixed
     */
    private function resetClient($client){
        $len = count($client);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $sid = $client['sid'];
                $findSid = $this->states()->findById(array('sid'=>$sid));
                $client['state'] = $findSid['state'];
            }
        }else{
            $client['state'] = '';
        }
        return $client;
    }

}